<?php
class lib_company_diaocha_log extends lib_base{
	public $table_name = "qc_company_diaocha_log";
    public $pk = "id";

	public function getList($cond,$h5=0){

        if($h5==1){
            $result = $this->get_loaddata($cond,'id desc','*',10);
        }else{
            $result = $this->get_pagelist($cond,'id desc','*',20);
        }
        if($result){
            $lists = $result['list'];

            foreach ($lists as $val) {

                if($val['admin_ids']){
                    $val['admin_ids'] = explode(',',$val['admin_ids']);
                }
                $list[] = $val;
            }


            $data['list'] = $list;
            $data['showPage'] = $result['showPage'];
            $data['total'] = $result['total'];

            return $data;
        }else{
            return false;
        }

    }

    public function add_log($order_id,$data){
		$order = D('company_order')->get_info($order_id);

		$msg = array(
    		'1'=>'发布了订单',
    		'2'=>'派送了订单',
    		'3'=>'接受了订单',
    		'4'=>'订单已完成',
    		'5'=>'订单已确认',
    		'6'=>'评价了订单',
    		'7'=>'结算了订单',
    		'99'=>'取消了订单',
    	);

    	$intro['msg'] = $msg[$order['order_status']];
        if($order['order_status'] > 1){
            // if($data['photo']) $intro['photo'] = joinStr($data['photo']);
			if($data['photo']){
				$intro['photo'] = json_decode($data['photo'],true);
			}
            if($data['intro']) $intro['intro'] = $data['intro'];
		}

    	if($order){
	        $arrBase = array(
				'company_id' =>$order['company_id'],
	        	'order_id' =>$order_id,
                'device_id' =>$order['device_id'],
				'intro' =>serialize($intro),
				'order_status' =>$order['order_status'],
				'status' =>1,
				'add_time'=>date('Y-m-d H:i:s'),
			);
			return $this->add($arrBase);
		}

    }






	// 满意度
    public function get_myd_zone($zone_ids,$startDate='',$endDate='')
    {
        $data = [];
        foreach ($zone_ids as $val) {
            $data[] = $this->get_myd_zone2($val,$startDate,$endDate);
        }
        return $data;
    }

    public function get_myd_zone2($zone_id=0,$startDate,$endDate)
    {
        $cond = "status=1 and zone_id='{$zone_id}' ";

        // 最近七日
		$s_time = date('Y-m-d 00:00:00', strtotime('-7 days'));
		$e_time = date("Y-m-d 00:00:00");
		if($startDate) $s_time = $startDate;
		if($endDate) $s_time = $endDate;
		$cond .= " and add_time>='{$s_time}' and add_time<='{$e_time}'";

		$count2 = $this->findCount(array($cond));

        // 满意度
        $res = $this->findAll(array($cond),'id desc','sum(score) as zscore');
        if($res){
            $zscore = $res[0]['zscore'];
            if($count2>0){
                $count3 = round($zscore / $count2,2);
            }
        }else{
            $count3 = 0;
        }
		$data['count2'] = number_format($count2);
		$data['count3'] = number_format($count3);

        return $data;
    }

	/**
     * 订单导出
     * @param $dataType
     * @param $query
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function outdata_myd_csv($zones,$data)
    {
        // 表格标题
        $tileArray = ['排名', '科室', '总调查数', '总体满意率'];
        // 表格内容
        $dataArray = [];

		$startDate='';
		$endDate='';
		if($data['time_s']){
			$startDate=$data['time_s'];
		}
		if($data['time_e']){
			$endDate=$data['time_e'];
		}
        if($zones){
            foreach($zones as $val){
                $v[$val['id']] = $this->get_myd_zone2($val['id'],$startDate,$endDate);
                $v[$val['id']]['name'] = $val['name'];
				$v[$val['id']]['zone_id'] = $val['id'];
            }
        }
        $cateRanking = $v;
        $count2 = array_column($cateRanking, 'count2');
        $count3 = array_column($cateRanking, 'count3');
        array_multisort($count3, SORT_DESC, $count2, SORT_DESC, $cateRanking);

        // $query = $this->setWhere($cond,$data);
        // 获取订单列表
        // $list = $this->findAll(array($query));
        if($cateRanking){
            // $this->areas = D('company_area')->ids($list,'area_id');
            // $this->cates = D('company_cate')->ids($list,'cate_id');
            $zones = D('company_zone')->ids($cateRanking,'zone_id');
            // $this->admins = D('company_admin')->ids($list,'admin_id');
            // $this->shifus = D('company_admin')->ids($list,'shifu_id');
            // $this->devices = D('company_device')->ids($list,'device_id');

            // $this->orderStatus = array('1'=>'新报修','2'=>'已派单','3'=>'已接单','4'=>'维修中','5'=>'已完成','6'=>'已评价','99'=>'已取消');

			$i = 1;
            foreach ($cateRanking as $val) {

                $dataArray[] = [
                    '排名' => $i,
                    '科室' => $zones[$val['zone_id']]['name'],
                    '总调查数' => $this->filterValue($val['count2']),
                    '总体满意率' => $this->filterValue($val['count3']).'%',
                ];

				$i = $i + 1;
            }

        }

         // 导出csv文件
         $filename = '科室满意度排行表-' . date('YmdHis');
         export_excel($filename . '.csv', $tileArray, $dataArray);
    }

    /**
     * 表格值过滤
     * @param $value
     * @return string
     */
    private function filterValue($value)
    {
        return "\t" . $value . "\t";
    }

    private function setWhere($cond,$query)
    {
        // $cond = $cond;
        if (isset($query['keyword']) && !empty($query['keyword'])) {
            $cond.=" and (log like '%".$query['keyword']."%') ";
        }
        if (isset($query['area_id']) && !empty($query['area_id'])) {
            $cond .=" and area_id='{$query['area_id']}'";
        }
        if (isset($query['cate_id']) && !empty($query['cate_id'])) {
            $cond .=" and cate_id='{$query['cate_id']}'";
        }
        if (isset($query['zone_id']) && !empty($query['zone_id'])) {
            $cond .=" and zone_id='{$query['zone_id']}'";
        }
        if (isset($query['device_id']) && !empty($query['device_id'])) {
            $cond .=" and device_id='{$query['device_id']}'";
        }
		if (isset($query['diaocha_id']) && !empty($query['diaocha_id'])) {
            $cond .=" and diaocha_id='{$query['diaocha_id']}'";
        }
        if (isset($query['shifu_id']) && !empty($query['shifu_id'])) {
            $cond .=" and shifu_id='{$query['shifu_id']}'";
        }
        if (isset($query['time_s']) && !empty($query['time_s'])) {
            $cond .=" and add_time>='{$query['time_s']}'";
        }
        if (isset($query['time_e']) && !empty($query['time_e'])) {
            $cond .=" and add_time<='{$query['time_e']}'";
        }
        if (isset($query['time_s2']) && !empty($query['time_s2'])) {
            $cond .=" and up_time>='{$query['time_s2']}'";
        }
        if (isset($query['time_e2']) && !empty($query['time_e2'])) {
            $cond .=" and up_time<='{$query['time_e2']}'";
        }
        if (isset($query['order_status']) && !empty($query['order_status'])) {
            $cond .=" and order_status='{$query['order_status']}'";
        }

        return $cond;
    }
}